import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;


public class Consumer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Consumer c = new Consumer();
		String s = "";
		while (true){
			s = c.receive();
			if (s!=null) System.out.println(s);
		}
	}
	
	public String receive()
	{
		try
		{
			Context ctx = new InitialContext();
			TopicConnectionFactory topicfactory = (TopicConnectionFactory)ctx.lookup("TopicConnectionFactory");
			Topic topic = (Topic)ctx.lookup("Messages");
			TopicConnection topicConn = topicfactory.createTopicConnection();
			TopicSession topicSess = topicConn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
			TopicSubscriber topicSub = topicSess.createSubscriber(topic);
			topicConn.start();
			TextMessage msg = (TextMessage) topicSub.receive();
			return msg.getText();
			
			// TODO: Lookup the TopicConnectionFactory
			// TODO: Lookup the Topic
			// TODO: Create a new Topic Connection
			// TODO: Start a new Topic Session
			// TODO: Create the subscriber
			// TODO: Start the topic connection
			// TODO: Receive the message
			// TODO: Return the message text
		}
		catch (Exception e)
		{		
			System.out.println("Exception:" + e.getMessage());
			e.printStackTrace();	
			return null;
			
		}		
	}	

}
